﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MJM.CBD.Conn;
using MJM.SPP.EntidadesNegocio;

namespace MJM.SPP.AccesoDatos
{
    public class ProxysAD
    {
        public int Insertar(ProxysEN objProxysEN)
        {
            int codigo=1;
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_i_T_MAES_SERPRO_Insertar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmServidor;
            SqlParameter prmPuerto,prmTipo;
            try
            {
                prmServidor = cmd.CreateParameter();
                prmServidor.ParameterName = "@vSERPRO_chDIRSER";
                prmServidor.SqlDbType = SqlDbType.VarChar;
                prmServidor.Value = objProxysEN.SERPRO_chDIRSER;
                cmd.Parameters.Add(prmServidor);

                prmPuerto = cmd.CreateParameter();
                prmPuerto.ParameterName = "@vSERPRO_chPUESER";
                prmPuerto.SqlDbType = SqlDbType.VarChar;
                prmPuerto.Value = objProxysEN.SERPRO_chPUESER;
                cmd.Parameters.Add(prmPuerto);

                prmTipo = cmd.CreateParameter();
                prmTipo.ParameterName = "@vSERPRO_inTIPPRO";
                prmTipo.SqlDbType = SqlDbType.Int;
                prmTipo.Value = objProxysEN.SERPRO_inTIPPRO;
                cmd.Parameters.Add(prmTipo);

                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
                codigo = 0;
            }

            return codigo;
        }

        public List<ProxysEN> Listar()
        {
            List<ProxysEN> lst = new List<ProxysEN>();
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_s_T_MAES_SERPRO_Listar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr;
            ProxysEN objProxysEN;
            try
            {
                cmd.Connection.Open();
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    objProxysEN = new ProxysEN();
                    objProxysEN.SERPRO_P_inID = dr.GetInt32(0);
                    objProxysEN.SERPRO_chDIRSER = dr.GetString(1);
                    objProxysEN.SERPRO_chPUESER = dr.GetString(2);
                    objProxysEN.SERPRO_inTIPPRO = dr.GetInt32(3);
                    objProxysEN.SERPRO_inFLAELI = dr.GetInt32(4);
                    lst.Add(objProxysEN);
                }
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                cmd.Dispose();
                conn.Dispose();
            }
            return lst;
        }

        public int Eliminar(int Id)
        {
            int codigo = 1;
            SqlConnection conn = new SqlConnection(Cadena.GetCadenaPadron());
            SqlCommand cmd = new SqlCommand("sp_u_T_MAES_SERPRO_Eliminar", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter prmId;
            try
            {
                prmId = cmd.CreateParameter();
                prmId.ParameterName = "@vSERPRO_P_inID";
                prmId.SqlDbType = SqlDbType.Int;
                prmId.Value = Id;
                cmd.Parameters.Add(prmId);
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                cmd.Dispose();
                conn.Dispose();
            }
            catch(Exception ex)
            {
                conn.Dispose();
                cmd.Dispose();
                codigo = 0;
            }
            return codigo;
        }
    }
}
